package com.retired_serviceman_management.controller;

import com.retired_serviceman_management.entity.EnterpriseInfo;
import com.retired_serviceman_management.entity.SoldierInfo;
import com.retired_serviceman_management.entity.VocationalInfo;
import com.retired_serviceman_management.service.Enterprise_Info_Service;
import com.retired_serviceman_management.service.Soldier_Info_Service;
import com.retired_serviceman_management.service.VocationalInfoService;
import com.retired_serviceman_management.util.ListUtil;
import com.retired_serviceman_management.util.Result;
import com.retired_serviceman_management.util.Soldier_util;
import com.retired_serviceman_management.util.Update_password_util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("soldier_info")
public class Soldier_Info_Controller {

    @Resource
    private Soldier_Info_Service soldier_info_service;

    @Resource
    private Enterprise_Info_Service enterprise_info_service;

    @Resource
    private VocationalInfoService vocationalInfoService;

    /*
    退役军人登录
     */
    @ResponseBody
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public Result admin_login(@RequestBody SoldierInfo soldierInfo, HttpServletRequest request) {
        Integer checkAccount = soldier_info_service.CheckAccount(soldierInfo.getMobile(), soldierInfo.getPassword());
        System.out.println("checkAccount = " + checkAccount);
        if (checkAccount == 0) {//账号密码都正确,返回到显示退役军人信息页面，并且查询
            return new Result("0000", "登录成功", soldierInfo.getMobile());
        } else if (checkAccount == 1) {//账号不存在
            return new Result("0001", "账号不存在，请重新输入！");
//            request.setAttribute("flag1", "账号不存在!!!");
//            return "/jsp/Soldier_login.jsp";
        } else {//账号存在，密码错误
            return new Result("0002", "密码错误，请重新输入!");
//            request.setAttribute("flag2", "密码错误!!!");
//            return "/jsp/Soldier_login.jsp";
        }
    }

    /**
     * 退役军人查看个人信息
     */
    @RequestMapping(value = "soldier_view/{mobile}", method = RequestMethod.GET)
    public String soldier_view(@PathVariable("mobile") String mobile, HttpServletRequest request) {
        ListUtil soldierInfo1 = soldier_info_service.queryByMobile(mobile);
        System.out.println("soldierInfo = " + soldierInfo1);
        request.setAttribute("soldierInfo", soldierInfo1);
        return "/jsp/Soldier_info.jsp";
    }

    /**
     * 退役军人通过手机号注册
     */
    @ResponseBody
    @RequestMapping(value = "register", method = RequestMethod.POST)
    public Result register(@RequestBody Soldier_util soldier_util) {
        System.out.println(soldier_util.getSoldierName() + "," + soldier_util.getMobile() + "," + soldier_util.getPassword() + "," + soldier_util.getPassword1());
        Integer integer = soldier_info_service.register(soldier_util.getSoldierName(), soldier_util.getMobile(), soldier_util.getPassword(), soldier_util.getPassword1());
        System.out.println("integer = " + integer);
        if (integer == 1) {
            return new Result("1011", "注册成功，请登录");
        } else if (integer == 2) {
            return new Result("1012", "账号已存在，请重新输入");
        } else if (integer == 3) {
            return new Result("1013", "两次输入的密码不同，请重新输入");
        }
        return new Result("1010", "注册失败，请重试");
    }

    /**
     * 通过手机号（账号）修改个人信息
     */
    @ResponseBody
    @RequestMapping(value = "Update_info", method = RequestMethod.POST)
    public Result Update_info(@RequestBody ListUtil listUtil) {
        Integer i = soldier_info_service.Update_Soldier(listUtil);
        if (i == 0) {
            return new Result("0000", "修改失败");
        } else if (i == 2) {
            return new Result("0002", "没有改变", listUtil.getMobile());
        } else return new Result("0001", "修改成功");
    }

    /**
     * 退役军人查看招聘企业信息
     */
    @RequestMapping(value = "View_Enterprise/{mobile}", method = RequestMethod.GET)
    public String View_Enterprise(@PathVariable("mobile") String mobile, HttpServletRequest request) {
        List<EnterpriseInfo> enterpriseInfos = enterprise_info_service.queryAll();
        request.setAttribute("enterpriseInfos", enterpriseInfos);
        request.setAttribute("mobile", mobile);
        return "/jsp/soldier_enterprise.jsp";
    }

    /**
     * 退役军人查看招聘企业详情
     */
    @RequestMapping(value = "Enterprise_detail/{comAccount}/{mobile}", method = RequestMethod.GET)
    public String Enterprise_detail(@PathVariable("comAccount") String comAccount, @PathVariable("mobile") String mobile, HttpServletRequest request) {
        VocationalInfo vocationalInfo = vocationalInfoService.Enterprise_detail(comAccount);
        request.setAttribute("mobile", mobile);
        if (vocationalInfo == null) {
            return "/jsp/404.jsp";
        } else {
            request.setAttribute("vocationalInfo", vocationalInfo);
            return "/jsp/Enterprise_detail.jsp";
        }
    }


    /**
     * 修改个人密码
     */
    @ResponseBody
    @RequestMapping(value = "Update_password", method = RequestMethod.POST)
    public Result Update_password(@RequestBody Update_password_util update_password_util) {
        System.out.println(update_password_util.getOldPassword() + "," + update_password_util.getNewPassword() + "," + update_password_util.getNewPasswordq() + "." + update_password_util.getMobile());
        Integer i = soldier_info_service.Update_password(update_password_util.getMobile(), update_password_util.getOldPassword(), update_password_util.getNewPassword(), update_password_util.getNewPasswordq());
        System.out.println("i = " + i);
        if (i == 1) {
            return new Result("1001", "修改成功");
        } else if (i == 0) {
            return new Result("1000", "修改失败");
        } else if (i == 2) {
            return new Result("1002", "原密码输入错误,请重新输入");
        } else if (i == 3) {
            return new Result("1003", "两次输入的密码不一致，请重新输入");
        }
        return new Result("4444", "网络错误，请重试");
    }
}
